import cv2
import numpy as np

img = cv2.imread("./img/up01.png")
color_binary = cv2.imread("./panel.png")
color_binary = cv2.cvtColor(color_binary,cv2.COLOR_BGR2GRAY)

img_shape = color_binary.shape
# 设置便宜量
offset_x = 160
offset_y = 0

# 图像大小 高：720 宽：1280
pts1 = np.float32([
        [img_shape[1] * 0.4, img_shape[0] * 0.7],
        [img_shape[1] * 0.6, img_shape[0] * 0.7],
        [img_shape[1] * 1 / 8, img_shape[0]],
        [img_shape[1] * 7 / 8, img_shape[0]]])

pts2 = np.float32([
        [offset_x, offset_y],
        [img_shape[1] - offset_x, offset_y],
        [offset_x, img_shape[0] - offset_y],
        [img_shape[1] - offset_x, img_shape[0] - offset_y]])
# pts2 = np.float32([[0, 0], [img_shape[1], 0], [0, img_shape[0]], [img_shape[1], img_shape[0]]])
pts = cv2.getPerspectiveTransform(pts1, pts2)  # 生成透视变换矩阵
correct_image = cv2.warpPerspective(color_binary, pts, (img_shape[1], img_shape[0]))

cv2.rectangle(correct_image,
                  [int(img_shape[1] * 0.4 + 20), int(img_shape[0] * 0.7)],
                  [int(img_shape[1] * 0.6 - 20), int(img_shape[0])],
                  color=(0,0,0),
                  thickness=cv2.FILLED)

cv2.imshow("img",correct_image)
cv2.waitKey(0)

# cv2.imwrite("panel2.png",correct_image)
# cv2.waitKey(0)